const app = getApp();
const wuhuUrl = app.globalData.wuhuUrl;
Page({
  data: {
    phone: '',
    code: '',
    phoneError: '',
    isGetCodeDisabled: true,
    getCodeText: '获取验证码',
    countdown: 60,
    timer: null
  },

  bindPhoneInput(e) {
    this.setData({
      phone: e.detail.value
    });
    this.validatePhone();
  },

  bindCodeInput(e) {
    this.setData({
      code: e.detail.value
    });
  },

  validatePhone() {
    const { phone } = this.data;
    const phoneError = !/^1[3-9]\d{9}$/.test(phone) ? '请输入有效的手机号' : '';
    this.setData({
      phoneError,
      isGetCodeDisabled: !!phoneError
    });
  },


  getCode() {
    if (this.data.isGetCodeDisabled) return;
    const mobile = this.data.phone;
    console.log(mobile)
    wx.request({
      url:wuhuUrl+'/ft/user/login-mobile-send', // 替换为你的接口地址
      method: 'GET',
      data: {
        mobile
      },
      success: (res) => {
        if (res.data.success) {
          this.setData({
            isGetCodeDisabled: true,
            getCodeText: `${this.data.countdown}秒`
          });

          this.data.timer = setInterval(() => {
            const { countdown } = this.data;
            if (countdown > 1) {
              this.setData({
                countdown: countdown - 1,
                getCodeText: `${countdown - 1}秒`
              });
            } else {
              clearInterval(this.data.timer);
              this.setData({
                countdown: 60,
                getCodeText: '获取验证码',
                isGetCodeDisabled: false
              });
            }
          }, 1000);
        } else {
          wx.showToast({
            title: res.data.message || '获取验证码失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '请求失败，请稍后重试',
          icon: 'none'
        });
      }
    });
  },

  login() {
    const { phone, code } = this.data;
    if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
      this.setData({
        phoneError: '请输入有效的手机号'
      });
      return;
    }
    if (!code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return;
    }

    wx.request({
      url: wuhuUrl+'/ft/user/login-mobile-verify', // 替换为你的接口地址
      method: 'GET',
      data: {
        mobile: phone,
        code: code
      },
      success: (res) => {
        if (res.data.success) {
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 2000,
            success: () => {
              wx.setStorageSync('userInfo', res.data.data);
              setTimeout(() => {
                wx.switchTab({
                  url: '/pages/index/index'
                });
              }, 2000);
            }
          });
        } else {
          wx.showToast({
            title: res.data.message || '验证码错误，请重试',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '请求失败，请稍后重试',
          icon: 'none'
        });
      }
    });
  }
});
